package com.huizhonglm.diap.presenters.wallet;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.bruce.common.utils.FileUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.CommonResponse;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.Status;
import com.huizhonglm.diap.api.model.wallet.WithdrawParams;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.ui.wallet.iView.WithdrawIView;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2015-07-07 11:19
 */
public class WithdrawPresenter {

    private WithdrawIView iView;

    public WithdrawPresenter(WithdrawIView iView) {
        this.iView = iView;
    }

    public void onWithdraw(String cardId, Double amount, String passwd) {
        WithdrawParams params = new WithdrawParams();
        params.userId = UserUtil.getUserId();
        params.userBankID = cardId;
        params.userMoneyNumber = amount;
        params.password = FileUtil.MD5(passwd);
        API.http().post("withdraw", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse resp = JSON.parseObject(result, CommonResponse.class);
                    if (resp != null && Status.SUCCESS.equals(resp.getStatus())){
                        iView.onWithdrawSuccess();
                    } else {
                        iView.onWithdrawFailure("提现失败，请联系客服");
                    }
                } else {
                    iView.onWithdrawFailure("提现失败，请联系客服");
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onWithdrawFailure("提现失败，请联系客服");
            }
        });
    }
}
